// wv-3.3 (c) 2008 by Alexander Klein, akx@gmx.net
// client JavaScript

// init HTML page contents
function WV_Init () {	
	WV_LoadTable();
	WV_LoadShoutbox();
}

// debug messaging 
function WV_Msg (msg) {
	$('WV_Footer').insert({top: '<p>' + msg + '</p>'});
}

// load data table
function WV_LoadTable () {
	// call backend to send data table HTML
	new Ajax.Request ('wvsup.php', {
	  method: 'POST',
	  parameters: { action: 'loadTable'},
	  onComplete: function (response) {    	
	    $('WV_Body').update(response.responseText);
	  },
	  onFailure: function (response) {
	    WV_Msg('Ajax error. WV_LoadTable() failed.');
	  }  
	});  	
}

// load shout box
function WV_LoadShoutbox () {
	// call backend to send shoutbox HTML
	new Ajax.Request ('wvsup.php', {
	  method: 'POST',
	  parameters: { action: 'loadShoutbox'},
	  onComplete: function (response) {    	
	    $('WV_ShoutboxBody').update(response.responseText);
	  },
	  onFailure: function (response) {
	    WV_Msg('Ajax error. WV_LoadShoutbox() failed.');
	  }  
	});  	
}

// toggle cell value
function WV_ToggleCellValue (callingElement) {
	// call backend to set next value in data values array
	// for given DOM element id (table cell)
	// reload afterwards
	new Ajax.Request ('wvsup.php', {
	  method: 'POST',
	  parameters: { action: 'toggleCellValue',
	  							cellId: callingElement.id
	  },
	  onCreate: function() {
	  	$(callingElement.id).update('...');
	  },
	  onComplete: function (response) { 
	 		WV_LoadTable();
	  },
	  onFailure: function (response) {
	    WV_Msg('Ajax error. WV_ToggleCellValue() failed.');
	  }  
	});  	
}

// post a shoutbox line
function WV_ShoutboxPost () {
	// set maximum line length (the backend handles up to ~900)
	if ($F('WV_ShoutboxInput').length > 250) {
		WV_Msg('Message too long. WV_ShoutboxPost() failed.');
	}
	else {
		// call backend to save shoutbox line and reload afterwards
		new Ajax.Request ('wvsup.php', {
		  method: 'POST',
		  parameters: { action: 'postShout',
		  							shoutText: $F('WV_ShoutboxInput')
		  },
		  onComplete: function (response) {  
		 		WV_LoadShoutbox();
		  },
		  onFailure: function (response) {
		    WV_Msg('Ajax error. WV_ShoutboxPost() failed.');
		  }  
		});  		
	}
}

// not used function to highlight a table cell on mouseover
function WV_HighlightCell (callingElement) {
	// do nothing
}

// not used function to remove highlight from table cell on mouseout
function WV_UnHighlightCell (callingElement) {
	// do nothing
}
